//
//  PSSochiCalculatorTests.m
//  PrefCore
//
//  Created by Sergey on 3/4/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "PSCalculator.h"

#define TARGET_POOL (PSScoreValue)35

@interface PSSochiCalculatorTests : XCTestCase {
    PSCalculator *calc;
    PSGame *game;
    PSGameSettings *settings;
}

@end

@implementation PSSochiCalculatorTests

-(void)setUp {
    [super setUp];
    calc = [PSCalculator calculatorForGameFlavor:kGameFlavorSochi];
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [PSGameSettings clearUserDefaults:ud];
    [ud synchronize];
    settings = [PSGameSettings settingsFromUserDefaults:ud];
    settings.poolSize = TARGET_POOL;
    game = [[PSGame alloc] init];
}

-(void)testInit {
    XCTAssertNotNil(calc);
}

-(void)testWriteToPoolWithAidOnePlayerCompleteFit {
    // Zero and one are closed; two needs five more to close
    [[game scoreForPlayerId:0].pool addValue:TARGET_POOL];
    [[game scoreForPlayerId:1].pool addValue:TARGET_POOL];
    [[game scoreForPlayerId:2].pool addValue:TARGET_POOL-5];
    // Zero plays seven, and adds four points
    [calc writeToPoolOfPlayer:game.players[0] game:game settings:settings value:4];
    // Two gets the points in his pool, while zero gets whists against two
    XCTAssertEqual([game scoreForPlayerId:2].pool.current, TARGET_POOL-1);
    XCTAssertEqual([game scoreForPlayerId:0].whists[2].current, 40);
}

-(void)testWriteToPoolWithAidOnePlayerPartialFitCompleteWriteoff {
    // Zero and one are closed; two needs five more to close
    [[game scoreForPlayerId:0].pool addValue:TARGET_POOL];
    // Give player zero some mountain to reduce
    [[game scoreForPlayerId:0].mountain addValue:3];
    [[game scoreForPlayerId:1].pool addValue:TARGET_POOL];
    [[game scoreForPlayerId:2].pool addValue:TARGET_POOL-3];
    // Zero plays seven, and adds four points
    [calc writeToPoolOfPlayer:game.players[0] game:game settings:settings value:4];
    // Two gets the points in his pool, while zero gets whists against two
    XCTAssertEqual([game scoreForPlayerId:2].pool.current, TARGET_POOL);
    XCTAssertEqual([game scoreForPlayerId:0].whists[2].current, 30);
    XCTAssertEqual([game scoreForPlayerId:0].mountain.current, 2);
}

-(void)testWriteToPoolWithAidOnePlayerPartialFitPartialWriteoff {
    // Zero and one are closed; two needs five more to close
    [[game scoreForPlayerId:0].pool addValue:TARGET_POOL];
    // Give player zero some mountain to reduce
    [[game scoreForPlayerId:0].mountain addValue:3];
    [[game scoreForPlayerId:1].pool addValue:TARGET_POOL];
    [[game scoreForPlayerId:2].pool addValue:TARGET_POOL-3];
    // Zero plays seven, and adds four points
    [calc writeToPoolOfPlayer:game.players[0] game:game settings:settings value:8];
    // Two gets the points in his pool, while zero gets whists against two
    XCTAssertEqual([game scoreForPlayerId:2].pool.current, TARGET_POOL);
    XCTAssertEqual([game scoreForPlayerId:0].whists[2].current, 30);
    XCTAssertEqual([game scoreForPlayerId:0].mountain.current, 0);
}

-(void)testWriteToPoolCloseAndAidBothPlayers {
    // Zero and two need one point each; one needs four points
    [[game scoreForPlayerId:0].pool addValue:TARGET_POOL-1];
    [[game scoreForPlayerId:1].pool addValue:TARGET_POOL-4];
    [[game scoreForPlayerId:2].pool addValue:TARGET_POOL-1];
    // Zero plays seven, and adds four points
    [calc writeToPoolOfPlayer:game.players[0] game:game settings:settings value:4];
    // Zero and two are closed
    XCTAssertEqual([game scoreForPlayerId:0].pool.current, TARGET_POOL);
    XCTAssertEqual([game scoreForPlayerId:2].pool.current, TARGET_POOL);
    // One needs two more points to close
    XCTAssertEqual([game scoreForPlayerId:1].pool.current, TARGET_POOL-2);
    // Zero wrote whists against both one and two
    XCTAssertEqual([game scoreForPlayerId:0].whists[1].current, 20);
    XCTAssertEqual([game scoreForPlayerId:0].whists[2].current, 10);
}

@end
